package org.jeecg.modules.erp.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.erp.base.entity.ErpLocation;
import org.jeecg.modules.erp.base.service.IErpLocationService;
import org.jeecg.modules.erp.base.vo.ErpLocationVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 库位表
 * @Author: jeecg-boot
 * @Date:   2024-05-18
 * @Version: V1.0
 */
@Api(tags="库位表")
@RestController
@RequestMapping("/base/erpLocation")
@Slf4j
public class ErpLocationController extends JeecgController<ErpLocation, IErpLocationService> {
	@Autowired
	private IErpLocationService erpLocationService;
	
	/**
	 * 分页列表查询
	 *
	 * @param erpLocation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库位表-分页列表查询")
	@ApiOperation(value="库位表-分页列表查询", notes="库位表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ErpLocationVo>> queryPageList(ErpLocationVo erpLocation,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		QueryWrapper<ErpLocationVo> queryWrapper = QueryGenerator.initQueryWrapper(erpLocation, req.getParameterMap());
		Page<ErpLocationVo> page = new Page<ErpLocationVo>(pageNo, pageSize);
		IPage<ErpLocationVo> pageList = erpLocationService.pagelist(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param erpLocation
	 * @return
	 */
	@AutoLog(value = "库位表-添加")
	@ApiOperation(value="库位表-添加", notes="库位表-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:erp_location:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ErpLocation erpLocation) {
		erpLocationService.save(erpLocation);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param erpLocation
	 * @return
	 */
	@AutoLog(value = "库位表-编辑")
	@ApiOperation(value="库位表-编辑", notes="库位表-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:erp_location:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ErpLocation erpLocation) {
		erpLocationService.updateById(erpLocation);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库位表-通过id删除")
	@ApiOperation(value="库位表-通过id删除", notes="库位表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:erp_location:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		erpLocationService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库位表-批量删除")
	@ApiOperation(value="库位表-批量删除", notes="库位表-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:erp_location:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.erpLocationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "库位表-通过id查询")
	@ApiOperation(value="库位表-通过id查询", notes="库位表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ErpLocation> queryById(@RequestParam(name="id",required=true) String id) {
		ErpLocation erpLocation = erpLocationService.getById(id);
		if(erpLocation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(erpLocation);
	}

	 /**
	  * 查询库位，仓库信息
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "库位表-通过id查询")
	 @ApiOperation(value="库位表-通过id查询", notes="库位表-通过id查询")
	 @GetMapping(value = "/queryInfoById")
	 public Result<ErpLocationVo> queryInfoById(@RequestParam(name="id",required=true) String id) {
		 ErpLocationVo info = erpLocationService.info(id);
		 if(info==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(info);
	 }


    /**
    * 导出excel
    *
    * @param request
    * @param erpLocation
    */
    //@RequiresPermissions("org.jeecg.modules.demo:erp_location:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ErpLocationVo erpLocation,  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		// Step.1 组装查询条件查询数据
		QueryWrapper<ErpLocationVo> queryWrapper = QueryGenerator.initQueryWrapper(erpLocation, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<ErpLocationVo> page = new Page<ErpLocationVo>(pageNo, pageSize);
		//配置选中数据查询条件
		String selections = request.getParameter("selections");
		if(oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("id",selectionList);
		}
		//Step.2 获取导出数据
		//pagelist
		IPage<ErpLocationVo> bussStorageNoticePageIPage = erpLocationService.pagelist(page, queryWrapper);
		List<ErpLocationVo> records = bussStorageNoticePageIPage.getRecords();
//      List<BussStorageNotice> bussStorageNoticeList = bussStorageNoticeService.list(queryWrapper);

		// Step.3 组装pageList
//      List<BussStorageNoticePage> pageList = new ArrayList<BussStorageNoticePage>();
//      for (BussStorageNoticePage main : records) {
//          BussStorageNoticePage vo = new BussStorageNoticePage();
//          BeanUtils.copyProperties(main, vo);
////          List<BussStorageNoticeCarinfoVo> bussStorageNoticeCarinfoList = bussStorageNoticeCarinfoService.selectByMainId(main.getId());
////          vo.setBussStorageNoticeCarinfoList(BeanPlusUtil.toBeanList(bussStorageNoticeCarinfoList,BussStorageNoticeCarinfo.class) );
////		  List<BussStorageNoticeMaterialVo> bussStorageNoticeMaterialVos = bussStorageNoticeMaterialService.selectByMainId(main.getId());
////		  vo.setBussStorageNoticeMaterialVoList(bussStorageNoticeMaterialVos);
//          pageList.add(vo);
//      }

		// Step.4 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "库位列表");
		mv.addObject(NormalExcelConstants.CLASS, ErpLocationVo.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("库位数据", "导出人:"+sysUser.getRealname(), "库位"));
		mv.addObject(NormalExcelConstants.DATA_LIST, records);
		return mv;
//        return super.exportXls(request, erpLocation, ErpLocation.class, "库位表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("erp_location:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ErpLocation.class);
    }

}
